/*
Copyright (c) 2020-2021, Huawei Technologies Co., Ltd
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

* Description: 2D图表可视化插件界面类
*/
#include "ui_st_widget.h"
#include <ros/ros.h>

namespace chart_plugin {
const QString WIDGET_NAME = QString::fromUtf8("widget");
const QString VERTICALL_LAYOUT_NAME = QString::fromUtf8("verticalLayout");
const QString QWT_PLOT_NAME = QString::fromUtf8("qwtPlot");

const int32_t WIDGET_WIDTH = 572;
const int32_t WIDGET_HEIGHT = 327;
const int32_t WIDGET_MIN_WIDTH = 320;
const int32_t WIDGET_MIN_HEIGHT = 260;

const int32_t QWTPLOT_MIN_WIDTH = 240;
const int32_t QWTPLOT_MIN_HEIGHT = 230;
const int32_t QWTPLOT_LINEWIDTH = 1;

const int32_t QSPACER_ITEM_WIDTH = 40;
const int32_t QSPACER_ITEM_HEIGHT = 20;

const int32_t QLABEL_MIN_WIDTH = 50;
const int32_t QLABEL_MIN_HEIGHT = 10;

const int32_t ACTIVE_WINDOW_TEXT_COLOR_R = 255;
const int32_t ACTIVE_WINDOW_TEXT_COLOR_G = 255;
const int32_t ACTIVE_WINDOW_TEXT_COLOR_B = 255;
const int32_t ACTIVE_WINDOW_TEXT_COLOR_ALPHA = 255;

const int32_t DISABLED_WINDOW_TEXT_COLOR_R = 10;
const int32_t DISABLED_WINDOW_TEXT_COLOR_G = 10;
const int32_t DISABLED_WINDOW_TEXT_COLOR_B = 12;
const int32_t DISABLED_WINDOW_TEXT_COLOR_ALPHA = 255;

const int32_t ACTIVE_BUTTON_COLOR_R = 20;
const int32_t ACTIVE_BUTTON_COLOR_G = 21;
const int32_t ACTIVE_BUTTON_COLOR_B = 24;
const int32_t ACTIVE_BUTTON_COLOR_ALPHA = 255;

const int32_t ACTIVE_LIGHT_COLOR_R = 30;
const int32_t ACTIVE_LIGHT_COLOR_G = 31;
const int32_t ACTIVE_LIGHT_COLOR_B = 36;
const int32_t ACTIVE_LIGHT_COLOR_ALPHA = 255;

const int32_t ACTIVE_MIDLIGHT_COLOR_R = 25;
const int32_t ACTIVE_MIDLIGHT_COLOR_G = 26;
const int32_t ACTIVE_MIDLIGHT_COLOR_B = 30;
const int32_t ACTIVE_MIDLIGHT_COLOR_ALPHA = 255;

const int32_t ACTIVE_DARK_COLOR_R = 10;
const int32_t ACTIVE_DARK_COLOR_G = 10;
const int32_t ACTIVE_DARK_COLOR_B = 12;
const int32_t ACTIVE_DARK_COLOR_ALPHA = 255;

const int32_t ACTIVE_MID_COLOR_R = 13;
const int32_t ACTIVE_MID_COLOR_G = 14;
const int32_t ACTIVE_MID_COLOR_B = 16;
const int32_t ACTIVE_MID_COLOR_ALPHA = 255;

const int32_t ACTIVE_TEXT_COLOR_R = 255;
const int32_t ACTIVE_TEXT_COLOR_G = 255;
const int32_t ACTIVE_TEXT_COLOR_B = 255;
const int32_t ACTIVE_TEXT_COLOR_ALPHA = 255;

const int32_t DISABLED_TEXT_COLOR_R = 10;
const int32_t DISABLED_TEXT_COLOR_G = 10;
const int32_t DISABLED_TEXT_COLOR_B = 12;
const int32_t DISABLED_TEXT_COLOR_ALPHA = 255;

const int32_t ACTIVE_BRIGHTTEXT_COLOR_R = 255;
const int32_t ACTIVE_BRIGHTTEXT_COLOR_G = 255;
const int32_t ACTIVE_BRIGHTTEXT_COLOR_B = 255;
const int32_t ACTIVE_BRIGHTTEXT_COLOR_ALPHA = 255;

const int32_t ACTIVE_BUTTONTEXT_COLOR_R = 255;
const int32_t ACTIVE_BUTTONTEXT_COLOR_G = 255;
const int32_t ACTIVE_BUTTONTEXT_COLOR_B = 255;
const int32_t ACTIVE_BUTTONTEXT_COLOR_ALPHA = 255;

const int32_t DISABLED_BUTTONTEXT_COLOR_R = 10;
const int32_t DISABLED_BUTTONTEXT_COLOR_G = 10;
const int32_t DISABLED_BUTTONTEXT_COLOR_B = 12;
const int32_t DISABLED_BUTTONTEXT_COLOR_ALPHA = 255;

const int32_t ACTIVE_BASE_COLOR_R = 0;
const int32_t ACTIVE_BASE_COLOR_G = 0;
const int32_t ACTIVE_BASE_COLOR_B = 0;
const int32_t ACTIVE_BASE_COLOR_ALPHA = 255;

const int32_t DISABLED_BASE_COLOR_R = 20;
const int32_t DISABLED_BASE_COLOR_G = 21;
const int32_t DISABLED_BASE_COLOR_B = 24;
const int32_t DISABLED_BASE_COLOR_ALPHA = 255;

const int32_t ACTIVE_WINDOW_COLOR_R = 20;
const int32_t ACTIVE_WINDOW_COLOR_G = 21;
const int32_t ACTIVE_WINDOW_COLOR_B = 24;
const int32_t ACTIVE_WINDOW_COLOR_ALPHA = 255;

const int32_t ACTIVE_SHADOW_COLOR_R = 0;
const int32_t ACTIVE_SHADOW_COLOR_G = 0;
const int32_t ACTIVE_SHADOW_COLOR_B = 0;
const int32_t ACTIVE_SHADOW_COLOR_ALPHA = 255;

const int32_t ACTIVE_ALTERNATE_BASE_COLOR_R = 10;
const int32_t ACTIVE_ALTERNATE_BASE_COLOR_G = 10;
const int32_t ACTIVE_ALTERNATE_BASE_COLOR_B = 12;
const int32_t ACTIVE_ALTERNATE_BASE_COLOR_ALPHA = 255;

const int32_t DISABLED_ALTERNATE_BASE_COLOR_R = 20;
const int32_t DISABLED_ALTERNATE_BASE_COLOR_G = 21;
const int32_t DISABLED_ALTERNATE_BASE_COLOR_B = 24;
const int32_t DISABLED_ALTERNATE_BASE_COLOR_ALPHA = 255;

const int32_t ACTIVE_TOOLTIPBASE_COLOR_R = 255;
const int32_t ACTIVE_TOOLTIPBASE_COLOR_G = 255;
const int32_t ACTIVE_TOOLTIPBASE_COLOR_B = 220;
const int32_t ACTIVE_TOOLTIPBASE_COLOR_ALPHA = 255;

const int32_t ACTIVE_TOOLTIPTEXT_COLOR_R = 0;
const int32_t ACTIVE_TOOLTIPTEXT_COLOR_G = 0;
const int32_t ACTIVE_TOOLTIPTEXT_COLOR_B = 0;
const int32_t ACTIVE_TOOLTIPTEXT_COLOR_ALPHA = 255;

const int32_t ACTIVE_PLACEHOLDERTEXT_COLOR_R = 255;
const int32_t ACTIVE_PLACEHOLDERTEXT_COLOR_G = 255;
const int32_t ACTIVE_PLACEHOLDERTEXT_COLOR_B = 255;
const int32_t ACTIVE_PLACEHOLDERTEXT_COLOR_ALPHA = 128;

void UiStWidget::SetWindowTextPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_WINDOW_TEXT_COLOR_R, ACTIVE_WINDOW_TEXT_COLOR_G, DISABLED_WINDOW_TEXT_COLOR_B,
        ACTIVE_WINDOW_TEXT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);

    brush.setColor(QColor(DISABLED_WINDOW_TEXT_COLOR_R, DISABLED_WINDOW_TEXT_COLOR_G, DISABLED_WINDOW_TEXT_COLOR_B,
        DISABLED_WINDOW_TEXT_COLOR_ALPHA));
    palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush);
}

void UiStWidget::SetButtonPalette(QPalette &palette) const
{
    QBrush brush(
        QColor(ACTIVE_BUTTON_COLOR_R, ACTIVE_BUTTON_COLOR_G, ACTIVE_BUTTON_COLOR_B, ACTIVE_BUTTON_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Button, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Button, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Button, brush);
}

void UiStWidget::SetLightPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_LIGHT_COLOR_R, ACTIVE_LIGHT_COLOR_G, ACTIVE_LIGHT_COLOR_B, ACTIVE_LIGHT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Light, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Light, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Light, brush);
}

void UiStWidget::SetMidlightPalette(QPalette &palette) const
{
    QBrush brush(
        QColor(ACTIVE_MIDLIGHT_COLOR_R, ACTIVE_MIDLIGHT_COLOR_G, ACTIVE_MIDLIGHT_COLOR_B, ACTIVE_MIDLIGHT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Midlight, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Midlight, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Midlight, brush);
}

void UiStWidget::SetDarkPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_DARK_COLOR_R, ACTIVE_DARK_COLOR_G, ACTIVE_DARK_COLOR_B, ACTIVE_DARK_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Dark, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Dark, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Dark, brush);
}

void UiStWidget::SetMidPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_MID_COLOR_R, ACTIVE_MID_COLOR_G, ACTIVE_MID_COLOR_B, ACTIVE_MID_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Mid, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Mid, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Mid, brush);
}

void UiStWidget::SetTextPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_TEXT_COLOR_R, ACTIVE_TEXT_COLOR_G, ACTIVE_TEXT_COLOR_B, ACTIVE_TEXT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Text, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Text, brush);

    brush.setColor(
        QColor(DISABLED_TEXT_COLOR_R, DISABLED_TEXT_COLOR_G, DISABLED_TEXT_COLOR_B, DISABLED_TEXT_COLOR_ALPHA));
    palette.setBrush(QPalette::Disabled, QPalette::Text, brush);
}

void UiStWidget::SetBrightTextPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_BRIGHTTEXT_COLOR_R, ACTIVE_BRIGHTTEXT_COLOR_G, ACTIVE_BRIGHTTEXT_COLOR_B,
        ACTIVE_BRIGHTTEXT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::BrightText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::BrightText, brush);
    palette.setBrush(QPalette::Disabled, QPalette::BrightText, brush);
}

void UiStWidget::SetButtonTextPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_BUTTONTEXT_COLOR_R, ACTIVE_BUTTONTEXT_COLOR_G, ACTIVE_BUTTONTEXT_COLOR_B,
        ACTIVE_BUTTONTEXT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::ButtonText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::ButtonText, brush);

    brush.setColor(QColor(DISABLED_BUTTONTEXT_COLOR_R, DISABLED_BUTTONTEXT_COLOR_G, DISABLED_BUTTONTEXT_COLOR_B,
        DISABLED_BUTTONTEXT_COLOR_ALPHA));
    palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);
}

void UiStWidget::SetBasePalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_BASE_COLOR_R, ACTIVE_BASE_COLOR_G, ACTIVE_BASE_COLOR_B, ACTIVE_BASE_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Base, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Base, brush);

    brush.setColor(
        QColor(DISABLED_BASE_COLOR_R, DISABLED_BASE_COLOR_G, DISABLED_BASE_COLOR_B, DISABLED_BASE_COLOR_ALPHA));
    palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
}

void UiStWidget::SetWindowPalette(QPalette &palette) const
{
    QBrush brush(
        QColor(ACTIVE_WINDOW_COLOR_R, ACTIVE_WINDOW_COLOR_G, ACTIVE_WINDOW_COLOR_B, ACTIVE_WINDOW_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Window, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Window, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Window, brush);
}

void UiStWidget::SetShadowPalette(QPalette &palette) const
{
    QBrush brush(
        QColor(ACTIVE_SHADOW_COLOR_R, ACTIVE_SHADOW_COLOR_G, ACTIVE_SHADOW_COLOR_B, ACTIVE_SHADOW_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Shadow, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Shadow, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Shadow, brush);
}

void UiStWidget::SetAlternateBasePalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_ALTERNATE_BASE_COLOR_R, ACTIVE_ALTERNATE_BASE_COLOR_G, ACTIVE_ALTERNATE_BASE_COLOR_B,
        ACTIVE_ALTERNATE_BASE_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::AlternateBase, brush);
    palette.setBrush(QPalette::Inactive, QPalette::AlternateBase, brush);

    brush.setColor(QColor(DISABLED_ALTERNATE_BASE_COLOR_R, DISABLED_ALTERNATE_BASE_COLOR_G,
        DISABLED_ALTERNATE_BASE_COLOR_B, DISABLED_ALTERNATE_BASE_COLOR_ALPHA));
    palette.setBrush(QPalette::Disabled, QPalette::AlternateBase, brush);
}

void UiStWidget::SetToolTipBasePalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_TOOLTIPBASE_COLOR_R, ACTIVE_TOOLTIPBASE_COLOR_G, ACTIVE_TOOLTIPBASE_COLOR_B,
        ACTIVE_TOOLTIPBASE_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::ToolTipBase, brush);
    palette.setBrush(QPalette::Inactive, QPalette::ToolTipBase, brush);
    palette.setBrush(QPalette::Disabled, QPalette::ToolTipBase, brush);
}

void UiStWidget::SetToolTipTextPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_TOOLTIPTEXT_COLOR_R, ACTIVE_TOOLTIPTEXT_COLOR_G, ACTIVE_TOOLTIPTEXT_COLOR_B,
        ACTIVE_TOOLTIPTEXT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::ToolTipText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::ToolTipText, brush);
    palette.setBrush(QPalette::Disabled, QPalette::ToolTipText, brush);
}

void UiStWidget::SetPlaceholderTextPalette(QPalette &palette) const
{
    QBrush brush(QColor(ACTIVE_PLACEHOLDERTEXT_COLOR_R, ACTIVE_PLACEHOLDERTEXT_COLOR_G, ACTIVE_PLACEHOLDERTEXT_COLOR_B,
        ACTIVE_PLACEHOLDERTEXT_COLOR_ALPHA));
    brush.setStyle(Qt::SolidPattern);

#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
    palette.setBrush(QPalette::Active, QPalette::PlaceholderText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::PlaceholderText, brush);
    palette.setBrush(QPalette::Disabled, QPalette::PlaceholderText, brush);
#endif
}

void UiStWidget::InitPalette(QPalette &palette) const
{
    SetWindowTextPalette(palette);
    SetButtonPalette(palette);
    SetLightPalette(palette);
    SetMidlightPalette(palette);
    SetDarkPalette(palette);
    SetMidPalette(palette);
    SetTextPalette(palette);
    SetBrightTextPalette(palette);
    SetButtonTextPalette(palette);
    SetBasePalette(palette);
    SetWindowPalette(palette);
    SetShadowPalette(palette);
    SetAlternateBasePalette(palette);
    SetToolTipBasePalette(palette);
    SetToolTipTextPalette(palette);
    SetPlaceholderTextPalette(palette);
}

void UiStWidget::SetupUi(QWidget &widget)
{
    if (widget.objectName().isEmpty()) {
        widget.setObjectName(WIDGET_NAME);
    }
    widget.resize(WIDGET_WIDTH, WIDGET_HEIGHT);
    widget.setMinimumSize(QSize(WIDGET_MIN_WIDTH, WIDGET_MIN_HEIGHT));

    QPalette palette;
    InitPalette(palette);
    widget.setPalette(palette);
    widget.setAutoFillBackground(true);
    
    verticalLayout = new QVBoxLayout(&widget);
    verticalLayout->setObjectName(VERTICALL_LAYOUT_NAME);
    
    qwtPlot = new QwtPlot(&widget);
    qwtPlot->setObjectName(QWT_PLOT_NAME);
    qwtPlot->setMinimumSize(QSize(QWTPLOT_MIN_WIDTH, QWTPLOT_MIN_HEIGHT));
    qwtPlot->setFrameShape(QFrame::NoFrame);
    qwtPlot->setFrameShadow(QFrame::Plain);
    qwtPlot->setLineWidth(QWTPLOT_LINEWIDTH);
    verticalLayout->addWidget(qwtPlot);
} // setupUi
} //  namespace chart_plugin
